package com.epi.controller;

import com.epi.domain.dto.UserLoginReq;
import com.epi.basic.model.ErrorCode;
import com.epi.basic.model.ResponseVO;
import com.epi.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dxy
 * @since 1.0
 */
@RestController
public class HelloControlller {
    @Autowired
    RedisCache redisCache;

    @GetMapping("/hello")
    public String hello() {
        return "Permit All";
    }

    @GetMapping("/anonymous")
    public String anonymous() {
        return "Only Anonymous";
    }

    @GetMapping("/redis/key")
    @PreAuthorize("hasAuthority('system:redis:list')")
    public ResponseVO getKey() {
        return ResponseVO.success(redisCache.getKeys("*"));
    }

    @GetMapping("/redis/add/{key}/{value}")
    @PreAuthorize("hasAuthority('system:redis:add')")
    public ResponseVO addKey(@PathVariable String key, @PathVariable String value) {
        redisCache.setCacheObject(key,value);
        return ResponseVO.success();
    }

    @GetMapping("/redis/del/{key}")
    @PreAuthorize("hasAuthority('system:redis:del')")
    public ResponseVO delKey(@PathVariable String key) {
        return ResponseVO.success(redisCache.deleteObject(key));
    }

    @GetMapping("/hi")
    public ResponseVO getUser() {
        return ResponseVO.from(ErrorCode.BAD_REQUEST, UserLoginReq.builder().username("zhangsan").password("123").build());
    }


    @GetMapping("/authority/has")
    @PreAuthorize("@ex.hasAuthority('1')")
    public ResponseVO testAuthority() {
        return ResponseVO.from(ErrorCode.BAD_REQUEST, UserLoginReq.builder().username("zhangsan").password("123").build());
    }

    @GetMapping("/authority/has2")
    public ResponseVO testAuthority2() {
        return ResponseVO.success(UserLoginReq.builder().username("zhangsan").password("123").build());
    }

}
